package com.zlkj.pro.core.file;

import cn.hutool.core.io.IoUtil;
import cn.stylefeng.roses.kernel.file.api.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.api.enums.BucketAuthEnum;
import cn.stylefeng.roses.kernel.file.api.exception.FileException;
import cn.stylefeng.roses.kernel.file.api.exception.enums.FileExceptionEnum;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSException;
import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class HuaweiFileOperator implements FileOperatorApi {
    private ObsClient obsClient;
    private final HuaweiProperties huaweiProperties;

    public HuaweiFileOperator(HuaweiProperties huaweiProperties) {
        this.huaweiProperties = huaweiProperties;
        this.initClient();
    }

    public void initClient() {
        String endPoint = this.huaweiProperties.getEndpoint();
        String accessKey = this.huaweiProperties.getAccessKey();
        String secretKey = this.huaweiProperties.getSecretKey();
        //this.qiniuClient = Auth.create(accessKey, secretKey);
        this.obsClient = new ObsClient(accessKey, secretKey, endPoint);

    }

    public void destroyClient() {
    }

    public Object getClient() {
        return this.obsClient;
    }

    public boolean doesBucketExist(String bucketName) {
        try {
            // 判断桶是否存在
            return obsClient.headBucket(bucketName);
        } catch (ClientException | OSSException var3) {
            throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, new Object[]{var3.getMessage()});
        }
    }

    public void setBucketAcl(String bucketName, BucketAuthEnum bucketAuthEnum) {
        this.setFileAcl(bucketName, "*", bucketAuthEnum);
    }

    public boolean isExistingFile(String bucketName, String key) {
        try {
            return this.obsClient.doesObjectExist(bucketName, key);
        } catch (ClientException | OSSException var4) {
            throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, new Object[]{var4.getMessage()});
        }
    }

    public void storageFile(String bucketName, String key, byte[] bytes) {
        if (bytes != null && bytes.length > 0) {
            try {
                obsClient.putObject(bucketName, key, new ByteArrayInputStream(bytes));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            throw new FileException(FileExceptionEnum.ERROR_FILE);
        }

    }

    public void storageFile(String bucketName, String key, InputStream inputStream) {
        if (inputStream != null) {
            byte[] bytes = IoUtil.readBytes(inputStream);
            obsClient.putObject(bucketName, key, new ByteArrayInputStream(bytes));
        }

    }

    public byte[] getFileBytes(String bucketName, String key) {
        InputStream objectContent = null;

        byte[] var5;
        try {
            // 流式下载
            ObsObject obsObject = obsClient.getObject(bucketName, key);
            // 读取对象内容
            InputStream input = obsObject.getObjectContent();
            var5 = IoUtil.readBytes(input);
            input.close();
        } catch (ClientException | IOException var9) {
            throw new FileException(FileExceptionEnum.WRITE_BYTES_ERROR, new Object[]{var9.getMessage()});
        } finally {
            IoUtil.close(objectContent);
        }

        return var5;
    }

    public void setFileAcl(String bucketName, String key, BucketAuthEnum bucketAuthEnum) {
        try {
            /*if (bucketAuthEnum.equals(BucketAuthEnum.PRIVATE)) {
                this.ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Private);
            } else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ)) {
                this.ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
            } else if (bucketAuthEnum.equals(BucketAuthEnum.PUBLIC_READ_WRITE)) {
                this.ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicReadWrite);
            }*/
        } catch (ClientException | OSSException var5) {
            throw new FileException(FileExceptionEnum.ALIYUN_FILE_ERROR, new Object[]{var5.getMessage()});
        }
    }

    public void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) {
        try {
            obsClient.copyObject(originBucketName, originFileKey, newBucketName, newFileKey);
        } catch (Exception var6) {
            throw new FileException(FileExceptionEnum.MINIO_FILE_ERROR, new Object[]{var6.getMessage()});
        }
    }

    public String getFileAuthUrl(String bucketName, String key, Long timeoutMillis) {
        return this.huaweiProperties.getBucket_endpoint() + File.separator + key;
    }

    public String getFileUnAuthUrl(String bucketName, String key) {
        return this.huaweiProperties.getBucket_endpoint() + File.separator + key;
    }

    public void deleteFile(String bucketName, String key) {
        try {
            //bucketManager.delete(bucketName, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
